import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/patrol/offline_submit_patrol_task_view_model.dart';

import '../../net/api/http_api.dart';
import '../../res/colors.dart';
import '../../util/request_url_utils.dart';
import '../../widgets/add_image_widget.dart';
import '../../widgets/title_value_widget.dart';

class OfflinePatrolDetailPage extends BaseStatefulMvvmWidget {
  static bool isOnTop = false;
  final String code;

  OfflinePatrolDetailPage(
    this.code,
  );

  @override
  State<StatefulWidget> createState() => _PatrolDetailState();
}

class _PatrolDetailState extends BaseMvvmState<OfflineSubmitPatrolTaskViewModel,
    OfflinePatrolDetailPage> {
  @override
  Widget buildLoadingContentView() {
    return viewModel.detailData == null
        ? Container()
        : ((viewModel.detailData?.status ?? 0) == 0 &&
                (viewModel.detailData?.executeDate??"").isNullOrEmpty())
            ? handleWidget()
            : patrolDetailWidget();
  }

  Widget handleWidget() {
    var data = viewModel.detailData;
    return Column(
      children: [
        if (!viewModel.hasNet)
          CommonText(viewModel.netTips,textColor: Colors.white,height: 1.5).addToContainer(
              width: double.infinity,
              padding: EdgeInsets.symmetric(vertical: 4,horizontal: 16),
              color: Colors.red),
        if(viewModel.hasNet&&viewModel.netTips.isNotEmpty)
          CommonText(viewModel.netTips,textColor: Colors.white,height: 1.5).addToContainer(
              width: double.infinity,
              padding: EdgeInsets.symmetric(vertical: 4,horizontal: 16),
              color: Colors.orange),
        ListView(
          padding: EdgeInsets.symmetric(horizontal: 16),
          children: [

            Container(
              height: 10,
            ),
            TitleValueWidget(
              "巡防区域：",
              text: data?.patrolRange ?? "",
              padding: EdgeInsets.zero,
              showArrow: false,
              hint: "",
              textAlign: TextAlign.end,
            ),
            Container(
              height: 10,
            ),
            TitleValueWidget(
              "巡防任务名称：",
              text: data?.patrolName ?? "",
              padding: EdgeInsets.zero,
              showArrow: false,
              hint: "",
              textAlign: TextAlign.end,
            ),
            Container(
              height: 10,
            ),
            TitleValueWidget(
              "巡防任务时间：",
              text: "${data?.startTime?.formatDateStr(format: [
                'HH',
                ':',
                'nn'
              ])}-${data?.endTime?.formatDateStr(format: ['HH', ':', 'nn'])}",
              padding: EdgeInsets.zero,
              showArrow: false,
              hint: "",
              textAlign: TextAlign.end,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                CommonText(viewModel.detailData?.pointName,
                    textSize: 17,
                    textColor: Colors.black,
                    fontWeight: FontWeight.w600),
                CommonText(viewModel.detailData?.statusName,
                    textSize: 15, textColor: Colours.app_main)
              ],
            ).addToContainer(margin: EdgeInsets.symmetric(vertical: 10)),
            Container(
              height: 10,
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CommonText('巡防要求：'),
                CommonText(viewModel.detailData?.require ?? "").setWeight(1)
              ],
            ),
            Container(
              height: 15,
            ),
            viewModel.selectHandleStatusWidget(),
            Container(
              height: 15,
            ),
            if (RequestUrlUtils.code == "zhylycs")
              AddAndUploadImageWidget(
                    (files, modelList) {
                  setState(() {
                    viewModel.fileStringLit = files;
                  });
                },
                files: viewModel.fileStringLit,
              ),
            CommonText('备注信息'),
            Container(
              height: 10,
            ),
            CommonInput(
              hintText: '请输入备注信息',
              isDense: true,
              padding: EdgeInsets.zero,
              keyboardType: CommonInputType.multiline,
              border: InputBorder.none,
              onChanged: (str) {
                viewModel.feedback = str;
              },
              maxLines: 7,
            ).addToContainer(
                decoration: BoxDecoration(
                  borderRadius: 8.borderRadius,
                  color: Colors.grey[200],
                ),
                padding: EdgeInsets.all(10)),
            ElevatedButton(
                style: ButtonStyle(
                    backgroundColor:
                    WidgetStateProperty.all(Colours.app_main),
                    minimumSize:
                    WidgetStateProperty.all(Size(double.infinity, 49))),
                onPressed: () {
                  viewModel.submitPatrol();
                },
                child: CommonText('提交并签到', textColor: Colors.white))
                .addToContainer(
                margin: EdgeInsets.symmetric(horizontal: 16, vertical: 15))
          ],
        ).setWeight(1)
      ],
    );
  }

  Widget patrolDetailWidget() {
    return ListView(
      padding: EdgeInsets.symmetric(horizontal: 16),
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            CommonText(viewModel.detailData?.pointName,
                textSize: 17,
                textColor: Colors.black,
                fontWeight: FontWeight.w600),
            CommonText(viewModel.detailData?.statusName,
                textSize: 15, textColor: Colors.green)
          ],
        ).addToContainer(margin: EdgeInsets.symmetric(vertical: 10)),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            CommonText('巡防要求：', height: 1.2),
            CommonText(viewModel.detailData?.require ?? "", height: 1.2)
                .setWeight(1)
          ],
        ),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CommonText('打卡时间：', height: 1.2),
            CommonText(viewModel.detailData?.executeDate ?? "", height: 1.2)
                .setWeight(1)
          ],
        ),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CommonText('巡防状态：', height: 1.2),
            CommonText(viewModel.detailData?.resultStatusName, height: 1.2)
                .setWeight(1)
          ],
        ),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CommonText('打卡人：', height: 1.2),
            CommonText(viewModel.detailData?.executeByName, height: 1.2)
                .setWeight(1)
          ],
        ),
        Container(
          height: 10,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            CommonText('备注：', height: 1.2),
            CommonText(viewModel.detailData?.feedback ?? "", height: 1.2)
                .setWeight(1)
          ],
        ),
        // if (viewModel.fileStringLit.isNotEmpty) ...[
        //   Container(
        //     height: 16,
        //   ),
        //   GridView.count(
        //     crossAxisCount: 3,
        //     crossAxisSpacing: 20,
        //     mainAxisSpacing: 20,
        //     shrinkWrap: true,
        //     physics: NeverScrollableScrollPhysics(),
        //     children: viewModel.fileStringLit
        //         .map((path) => ImageLoad(
        //               "${HttpApi.imageHead}$path",
        //               fit: BoxFit.cover,
        //             ).toRound(borderRadius: 4.borderRadius).onTap(() {
        //               Go().push(ShowImagePage(
        //                 viewModel.fileStringLit
        //                     .map((e) => "${HttpApi.imageHead}$path")
        //                     .toList(),
        //                 image: path,
        //               ));
        //             }))
        //         .toList(),
        //   )
        // ]
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text('巡防点详情'),
        actions: [
          viewModel?.detailList != null && viewModel.detailList.length > 0
              ? IconButton(
                  onPressed: () {
                    viewModel.changeDetail();
                  },
                  icon: Icon(Icons.switch_left))
              : Container()
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  OfflineSubmitPatrolTaskViewModel createViewModel() {
    return OfflineSubmitPatrolTaskViewModel(context, code: widget.code);
  }

  @override
  void onRetryClick() {}

  @override
  void onCreate() {
    OfflinePatrolDetailPage.isOnTop = true;
    super.onCreate();
  }

  @override
  void dispose() {
    OfflinePatrolDetailPage.isOnTop = false;
    super.dispose();
  }
}
